﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.TestManagement.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;
using System.Security.Cryptography;
using System.Reflection;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.IO;
using System.Xml;

namespace TFSServerProvider
{
    public static class TFSQueryFileProvider
    {
        static string TeamFoundationServer = "";
        static string TeamProject = "";

        //retired
        private static QueryDefinition GetQueryFromWiqFile(string wiqFilePath)
        {
            FileInfo wiq = new FileInfo(wiqFilePath);
            string Wiql = "";
            TeamFoundationServer = MSCISDevMetricHelper.CommonHelper.ReadInfoXmlSingleNode(wiq, "/WorkItemQuery/TeamFoundationServer");
            TeamProject = MSCISDevMetricHelper.CommonHelper.ReadInfoXmlSingleNode(wiq, "/WorkItemQuery/TeamProject");
            Wiql = MSCISDevMetricHelper.CommonHelper.ReadInfoXmlSingleNode(wiq, "/WorkItemQuery/Wiql");
            Wiql = Wiql.ToLower().Replace("@project", "'" + TeamProject + "'").Replace("&gt;", ">").Replace("&lt;", "<");
            QueryDefinition query = new QueryDefinition("BugList", Wiql);
            return query;
        }

        private static QueryDefinition GetQueryFromWiqFile(string wiqFilePath, string workItemFileName)
        {
            FileInfo wiq = new FileInfo(wiqFilePath);
            string Wiql = "";
            TeamFoundationServer = MSCISDevMetricHelper.CommonHelper.ReadInfoXmlSingleNode(wiq, "/WorkItemQuery/TeamFoundationServer");
            TeamProject = MSCISDevMetricHelper.CommonHelper.ReadInfoXmlSingleNode(wiq, "/WorkItemQuery/TeamProject");
            Wiql = MSCISDevMetricHelper.CommonHelper.ReadInfoXmlSingleNode(wiq, "/WorkItemQuery/Wiql");
            Wiql = Wiql.ToLower().Replace("@project", "'" + TeamProject + "'").Replace("&gt;", ">").Replace("&lt;", "<");
            QueryDefinition query = new QueryDefinition(workItemFileName, Wiql);
            return query;
        }

        public static DataTable GetItemFromTFSQueryFileToDataTable(string wiqFilePath)
        {
            QueryDefinition query = GetQueryFromWiqFile(wiqFilePath);
            TFSServerProvider tfs = new TFSServerProvider(TeamFoundationServer);
            return tfs.GetItemFromQueryToDataTable(query);
        }

        public static WorkItemCollection GetItemFromTFSQueryFile(string wiqFilePath)
        {
            QueryDefinition query = GetQueryFromWiqFile(wiqFilePath);
            TFSServerProvider tfs = new TFSServerProvider(TeamFoundationServer);
            return tfs.GetItemFromQuery(query);
        }

        //retired
        public static void GetItemFromTFSQueryFileToFile(string wiqFilePath, string outputFile)
        {
            QueryDefinition query = GetQueryFromWiqFile(wiqFilePath);
            TFSServerProvider tfs = new TFSServerProvider(TeamFoundationServer);
            tfs.GetItemFromQueryToFile(query, outputFile);
        }


        public static void GetItemFromTFSQueryFileToFile(string wiqFilePath, string outputFile,string workItemFileName)
        {
            QueryDefinition query = GetQueryFromWiqFile(wiqFilePath, workItemFileName);
            TFSServerProvider tfs = new TFSServerProvider(TeamFoundationServer);
            tfs.GetItemFromQueryToFile(query, outputFile);
        }
    }
}
